<template>
    <div class="login">
        <div class=" login-container" v-if="hide">
            <div class="login-wrap">
                <div class="logo">
                    <img src="../assets/image/logo.png" />
                    <p>奢批数字供应链系统</p>
                </div>
                <ul class="dlfs-ul">
                    <li :class="activeClass == index ? 'active' : ''" v-for="(itme, index) in loginTab" :key="index" @click="onTab(index)">
                        {{ itme }}
                    </li>
                </ul>
                <div class="dlfs-content">
                    <el-form v-show="signUpTab == false" class="" el-form ref="loginForm" :model="loginForm" :rules="loginRules">
                        <div v-if="activeClass == 0" class="dlfs-wrap clearFix" style="display: block;">
                            <el-form-item class="layui-form-item margin-top-40" el-form-item prop="username">
                                <el-input required v-model.trim="loginForm.username" placeholder="输入手机号/邮箱" maxlength="50" class="layui-input inputStyle" lay-verType="msg" />
                            </el-form-item>
                            <div class="layui-form-item margin-top-60">
                                <el-form-item prop="password">
                                    <el-input type="password" v-model.trim="loginForm.password" required placeholder="输入密码" maxlength="20" class="layui-input inputStyle" lay-verType="msg" @keyup.enter.native="toLogin()" />
                                </el-form-item>
                            </div>
<!--                            <div class="layui-form-item login-auto margin-top-30" style="height: 20px;">-->
<!--                                <div class="layui-input-inline" style="margin-left: 30px;">-->
<!--                                    <el-checkbox v-model="checked1">自动登录</el-checkbox>-->
<!--                                </div>-->
<!--                            </div>-->
                            <el-button type="primary" @click="handleLogin('loginForm')" class="login-btn margin-top-30">登录</el-button>
                            <div style="width: 25rem;box-sizing: border-box;overflow:hidden;margin: 30px auto 10px auto;">
                                <p @click="(signUpTab = true), (hide = false)" class="wjmm fl" style="line-height: 20px; margin-right: 20px;color:#409EFF ;">
                                    注册账户
                                </p>
                                <p
                                    @click="
                                        hide = false;
                                        isshow = true;
                                    "
                                    class="fr"
                                    style="cursor: pointer;font-size: 14px;line-height: 20px;display:block;color:#909399"
                                >
                                    忘记密码
                                </p>
                            </div>
                            <!-- <div class="layui-form-item login-qtfs margin-top-60">
                            <div class=" qtfs">
                                其他登录方式
                                <a href=""><img src="../assets/image/weixin.png" /></a>
                                <a href=""><img src="../assets/image/qq.png" /></a>
                                <a href=""><img src="../assets/image/weibo.png" /></a>
                            </div>
                            <p @click="(signUpTab = true), (hide = false)" class="wjmm" style="margin-left: -2px;">注册账户</p>
                        </div> -->
                        </div>
                    </el-form>
                    <el-form v-show="signUpTab == false" class="" el-form ref="loginForm1" :model="loginForm1" :rules="loginRules1">
                        <div v-if="activeClass == 1" class="dlfs-wrap clearFix" style="display: block;">
                            <div id="qrcode" style=""></div>
                            <p style="text-align: center;font-size: 18px;margin: 45px 0 40px 0;">
                                请使用微信扫描二维码登录
                            </p>
                            <div style="width: 25rem;box-sizing: border-box;overflow:hidden;margin: 30px auto 10px auto;">
                                <p @click="(signUpTab = true), (hide = false)" class="wjmm fl" style="line-height: 20px; margin-right: 20px;color:#409EFF ;">
                                    注册账户
                                </p>
                                <p
                                    @click="
                                        hide = false;
                                        isshow = true;
                                    "
                                    class="fr"
                                    style="cursor: pointer;font-size:14px;display:block;color:#909399 "
                                >
                                    忘记密码
                                </p>
                            </div>
                            <!-- <el-form-item class="layui-form-item margin-top-40" el-form-item prop="phone">
                            <el-input
                                required
                                v-model.trim="loginForm1.phone"
                                placeholder="输入手机号"
                                maxlength="11"
                                onKeypress="return(/[\d]/.test(String.fromCharCode(event.keyCode)))"
                                class="layui-input inputStyle"
                                lay-verType="msg"
                            />
                        </el-form-item>
                        <div class="layui-form-item margin-top-60">
                            <el-form-item class="verification" prop="VerificationCode">
                                <el-input
                                    v-model.trim="loginForm1.VerificationCode"
                                    required
                                    placeholder="输入验证码"
                                    maxlength="6"
                                    class="layui-input inputStyle"
                                    lay-verType="msg"
                                />
                                <p href="" class="hqyzm" @click="getCode()">获取验证码</p>
                            </el-form-item>
                        </div>
                        <div class="layui-form-item login-auto margin-top-30">
                            <div class="layui-input-inline"><el-checkbox v-model="checked2">自动登录</el-checkbox></div>
                            <router-link to="" class="fr wjmm mr50" style="line-height: 20px;">忘记密码</router-link>
                        </div>
                        <el-button type="primary" @click="handleLogin1('loginForm1')" class="login-btn">登录</el-button> -->
                            <!--  <div class="layui-form-item login-qtfs margin-top-60">
                            <div class="layui-input-inline qtfs">
                                <span>其他登录方式</span>
                                <a href=""><img src="../assets/image/weixin.png" /></a>
                                <a href=""><img src="../assets/image/qq.png" /></a>
                                <a href=""><img src="../assets/image/weibo.png" /></a>
                            </div>
                            <p @click="(signUpTab = true), (hide = false)" class="wjmm" style="margin-top: 8px;">注册账户</p>
                        </div> -->
                        </div>
                    </el-form>
                    <el-form v-show="signUpTab == false" class="" el-form ref="loginForm2" :model="loginForm2" :rules="loginRules2">
                        <div v-if="activeClass == 2" class="dlfs-wrap clearFix" style="display: block;">
                            <img src="../assets/image/ewm.jpg" style="width: 180px;height: 180px;margin:45px auto 0 auto;display: block;" alt="" srcset="" />
                            <p style="text-align: center;font-size: 18px;margin: 45px 0 40px 0;">
                                请使用QQ扫描二维码登录
                            </p>
                            <div style="width: 25rem;box-sizing: border-box;overflow:hidden;margin: 30px auto 10px auto;">
                                <p @click="(signUpTab = true), (hide = false)" class="wjmm fl" style="line-height: 20px; margin-right: 20px;color:#409EFF ;">
                                    注册账户
                                </p>
                                <p
                                    @click="
                                        hide = false;
                                        isshow = true;
                                    "
                                    class="fr"
                                    style="cursor: pointer;font-size:14px;line-height: 20px;display:block;color:#909399 "
                                >
                                    忘记密码
                                </p>
                            </div>
                            <!-- <el-form-item class="layui-form-item margin-top-40" el-form-item prop="email">
                            <el-input required v-model.trim="loginForm2.email" placeholder="输入邮箱" maxlength="30" class="layui-input inputStyle" lay-verType="msg" />
                        </el-form-item>
                        <div class="layui-form-item margin-top-60">
                            <el-form-item class="verification" prop="password">
                                <el-input
                                    v-model.trim="loginForm2.password"
                                    type="password"
                                    required
                                    maxlength="20"
                                    placeholder="输入密码"
                                    class="layui-input inputStyle"
                                    lay-verType="msg"
                                />
                            </el-form-item>
                        </div>
                        <div class="layui-form-item login-auto margin-top-30">
                            <div class="layui-input-inline"><el-checkbox v-model="checked3">自动登录</el-checkbox></div>
                            <router-link to="" class="fr wjmm mr50" style="line-height: 20px;">忘记密码</router-link>
                        </div>
                        <el-button type="primary" @click="handleLogin2('loginForm2')" class="login-btn">登录</el-button>
                        <div class="layui-form-item login-qtfs margin-top-60">
                            <div class="layui-input-inline qtfs">
                                <span>其他登录方式</span>
                                <a href=""><img src="../assets/image/weixin.png" /></a>
                                <a href=""><img src="../assets/image/qq.png" /></a>
                                <a href=""><img src="../assets/image/weibo.png" /></a>
                            </div>
                            <p @click="(signUpTab = true), (hide = false)" class="wjmm" style="margin-top: 8px;">注册账户</p>
                        </div> -->
                        </div>
                    </el-form>
                </div>
            </div>
        </div>
        <signup v-if="signUpTab" @showInsurInformation="insuranceInformations"></signup>
        <forget v-if="isshow" @displays="displays"></forget>
    </div>
</template>
<script>
import { getCodeImg, getCode, phoneSignIn, emailSignIn, getInfo } from '@/api/login';
import Cookies from 'js-cookie';
import { encrypt, decrypt } from '@/utils/jsencrypt';
import signup from '../components/signUp/index';
import forget from './Forget/index.vue';
import $ from 'jquery';
var QRCode = require('qrcode');

export default {
    name: 'Login',
    data() {
        var checkPhone = (rule, value, callback) => {
            if (!value) {
                return callback(new Error('手机号不能为空'));
            } else {
                const reg = /^1[3|4|5|7|8][0-9]\d{8}$/;
                //  console.log(reg.test(value));
                if (reg.test(value)) {
                    callback();
                } else {
                    return callback(new Error('请输入正确的手机号'));
                }
            }
        };
        return {
            codeUrl: '',
            cookiePassword: '',
            // 账户_登录
            loginForm: {
                username: '',
                password: '',
                rememberMe: false,
                uuid: ''
            },
            // 手机号_登录
            loginForm1: {
                phone: '',
                VerificationCode: ''
            },
            // 邮箱_登录
            loginForm2: {
                email: '',
                password: ''
            },
            // 账户验证
            loginRules: {
                username: [
                    {
                        required: true,
                        trigger: 'blur',
                        message: '手机号/邮箱不能为空'
                    },
                    {
                        min: 5,
                        max: 50,
                        message: '手机号/邮箱长度需在 5 到 50 个字符之间',
                        trigger: 'blur'
                    }
                ],
                password: [
                    {
                        required: true,
                        trigger: 'blur',
                        message: '密码不能为空'
                    },
                    {
                        required: true,
                        trigger: 'blur',
                        min: 6,
                        max: 20,
                        message: '密码需长度在 6 到 20 个字符之间'
                    }
                ]
            },
            // 手机号验证
            loginRules1: {
                phone: [
                    {
                        required: true,
                        trigger: 'blur',
                        message: '手机号不能为空'
                    },
                    { validator: checkPhone, trigger: 'blur' },
                    {
                        pattern: /^[A-Za-z0-9u4e00-u9fa5]+$/,
                        message: '不允许输入空格等特殊符号'
                    }
                ],
                VerificationCode: [
                    {
                        required: true,
                        trigger: 'blur',
                        message: '验证码不能为空'
                    },
                    {
                        pattern: /^[A-Za-z0-9u4e00-u9fa5]+$/,
                        message: '不允许输入空格等特殊符号'
                    }
                ]
            },
            // 邮箱验证
            loginRules2: {
                email: [{ required: true, trigger: 'blur', message: '邮箱不能为空' }],
                password: [{ required: true, trigger: 'blur', message: '密码不能为空' }]
            },

            loading: false,
            redirect: undefined,
            //
            // loginTab: ["账户登录", "微信登录", "QQ登录"],
            loginTab: ['账户登录'],
            activeClass: 0,
            signUpTab: false,
            hide: true,
            checked1: true,
            checked2: true,
            checked3: true,
            isshow: false
        };
    },
    components: {
        signup,
        forget
    },
    watch: {
        $route: {
            handler: function(route) {
                this.redirect = route.query && route.query.redirect;
            },
            immediate: true
        }
    },
    created() {
        this.getCookie();
        this.loginForm.username = localStorage.userName;
        this.loginForm.password = localStorage.userPassword;
        // 二维码
        // this.$http.get("http://8d0a0d490069.ngrok.io/weixin/GetCode").then(
        //     res => {
        //         console.log(res);
        //     },
        //     response => {}
        // );
        // if (localStorage.userName != "" && localStorage.userPassword != "") {
        //     this.loginForm.username = localStorage.userName;
        //     this.loginForm.password = localStorage.userPassword;
        //     this.$store
        //         .dispatch("Login", this.loginForm)
        //         .then(() => {
        //             this.info();
        //             this.$router.push({ path: this.redirect || "/" });
        //         })
        //         .catch(() => {
        //             this.loading = false;
        //         });
        // }

        // this.$router.push('/binding')
    },
    methods: {
        // 获取验证码(手机号-登录)

        getCookie() {
            const username = Cookies.get('username');
            const password = Cookies.get('password');
            const rememberMe = Cookies.get('rememberMe');
            this.loginForm = {
                username: username === undefined ? this.loginForm.username : username,
                password: password === undefined ? this.loginForm.password : decrypt(password),
                rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
            };
        },
        // 登录
        toLogin() {
            this.loading = true;
            if (this.loginForm.rememberMe) {
                Cookies.set('username', this.loginForm.username, {
                    expires: 30
                });
                Cookies.set('password', encrypt(this.loginForm.password), {
                    expires: 30
                });
                Cookies.set('rememberMe', this.loginForm.rememberMe, {
                    expires: 30
                });
            } else {
                Cookies.remove('username');
                Cookies.remove('password');
                Cookies.remove('rememberMe');
            }

            this.$store
                .dispatch('Login', this.loginForm)
                .then(() => {
                    // this.info();
                    this.$router.push({ path: this.redirect || '/' });
                })
                .catch(() => {
                    this.loading = false;
                });
            if ((this.checked1 = 'true')) {
                localStorage.userName = this.loginForm.username;
                localStorage.userPassword = this.loginForm.password;
            }
        },
        // 账户登录
        handleLogin() {
            this.$refs.loginForm.validate(valid => {
                if (valid) {
                    this.toLogin();
                }
            });
        },
        // 手机号登录
        // handleLogin1() {
        //     this.$refs.loginForm1.validate(valid => {
        //         if (valid) {
        //             this.loading = true;
        //             if (this.loginForm.rememberMe) {
        //                 Cookies.set("username", this.loginForm.username, {
        //                     expires: 30
        //                 });
        //                 Cookies.set(
        //                     "password",
        //                     encrypt(this.loginForm.password),
        //                     {
        //                         expires: 30
        //                     }
        //                 );
        //                 Cookies.set("rememberMe", this.loginForm.rememberMe, {
        //                     expires: 30
        //                 });
        //             } else {
        //                 Cookies.remove("username");
        //                 Cookies.remove("password");
        //                 Cookies.remove("rememberMe");
        //             }
        //             let datas = {
        //                 phone: this.loginForm1.phone,
        //                 phoneCode: this.loginForm1.VerificationCode
        //             };
        //             phoneSignIn(datas).then(res => {
        //                 console.log(res);
        //             });
        //         }
        //     });
        // },
        // // 邮箱登录
        // handleLogin2(loginForm) {
        //     this.$refs.loginForm2.validate(valid => {
        //         if (valid) {
        //             this.loading = true;
        //             if (this.loginForm.rememberMe) {
        //                 // Cookies.set('username', this.loginForm.username, { expires: 30 });
        //                 // Cookies.set('password', encrypt(this.loginForm.password), {
        //                 //     expires: 30
        //                 // });
        //                 // Cookies.set('rememberMe', this.loginForm.rememberMe, {
        //                 //     expires: 30
        //                 // });
        //             } else {
        //                 // Cookies.remove('username');
        //                 // Cookies.remove('password');
        //                 // Cookies.remove('rememberMe');
        //             }
        //             // this.$store
        //             //     .dispatch('Login', this.loginForm)
        //             //     .then(() => {
        //             //         this.$router.push({ path: this.redirect || '/' });
        //             //     })
        //             //     .catch(() => {
        //             //         this.loading = false;
        //             //         this.getCode();
        //             //     });
        //             let datas = {
        //                 email: this.loginForm2.email,
        //                 emailCode: this.loginForm2.password
        //             };
        //             emailSignIn(datas).then(res => {
        //                 console.log(res);
        //             });
        //         }
        //     });
        // },
        onTab(index) {
            this.activeClass = index;
            //console.log(index);
        },
        insuranceInformations() {
            this.signUpTab = false;
            this.hide = true;
        },
        displays() {
            this.hide = true;
            this.isshow = false;
        }

        // getinfo() {
        //     getInfo().then(res => {
        //         console.log(res);
        //         // 认证状态 0未提交 1 提交审核 2 认证通过 3 认证不通过',
        //         if (res.ApplyStatus == 0) {
        //             this.isshow = false;
        //             this.test = '请点击进入资质审核提交资料';
        //         } else if (res.ApplyStatus == 1) {
        //             this.isshow = false;
        //             this.test = '已提交审核请等待审核结果';
        //         } else if (res.ApplyStatus == 3) {
        //             this.isshow = false;
        //             this.test = '审核未通过，请联系客服';
        //         } else {
        //             this.isshow = true;
        //         }
        //     });
        // },
        // info() {
        //     getInfo().then(res => {
        //         console.log(res);
        //         this.$store.state.user.toPath = res.ApplyStatus;
        //         // 认证状态 0未提交 1 提交审核 2 认证通过 3 认证不通过',
        //         // this.$store.state.toPaths
        //         // if (res.hasShop == true) {
        //         //     this.$store.state.user.toPath = true
        //         // }else {
        //         //     this.$store.state.user.toPath = false
        //         // }
        //         // if (res.hasShop == false) {
        //         //     // alert(21222)
        //         //     this.isshow = false;
        //         // }
        //     });
        // }
    },
    mounted() {
        // 二维码
        QRCode.toDataURL('https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxb09dce1097c41a72&redirect_uri=http://8d0a0d490069.ngrok.io/weixin/getAccessToken&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect')
            .then(url => {
                var qrbox = document.querySelector('#qrcode');
                const img = new Image();
                img.src = url;
                qrbox.appendChild(img);
            })
            .catch(err => {
                console.error(err);
            });
    }
};
</script>

<style rel="stylesheet/scss" lang="scss">
@import '../assets/styles/global.css';
.login {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100%;
}
/* .title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;
  .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  colo r: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
} */
/* 登录 */
body {
    font-size: 62.5% !important;
}

.margin-top-30 {
    margin-top: 1.875rem !important;
}

.login-container {
    /* width: 1920px;
	height: 1080px; */
    width: 100%;
    height: 100%;
    min-height: 760px;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    background-image: url('../assets/image/login-bg.png');
    background-size: cover;
}

.login-wrap {
    /* width:714px;
	height:883px; */
    width: 44.625rem;
    /* height: 55.1875rem; */
    padding-bottom: 1.875rem;
    background: #ffffff;
    border-radius: 2.5rem;
    border: 0.375rem solid rgba(255, 255, 255, 0.31);
    float: right;
    /* margin-top: 6.125rem; */
    margin-right: 12.5rem;
}

.login-wrap .logo {
    text-align: center;
    padding-top: 2.9375rem;
}
.login-wrap .logo img {
    width: 90px;
    height: 90px;
}
.login-wrap .logo p {
    font-size: 2.375rem;
    font-family: SourceHanSansCN-Bold, SourceHanSansCN;
    font-weight: bold;
    color: rgba(8, 20, 38, 1);
    margin-top: 10px;
}

.dlfs-ul {
    text-align: center;
    margin-top: 3.5625rem;
}

.dlfs-ul li {
    display: inline-block;
    _display: initial;
    *display: initial;
    *zoom: 1;
    color: #bababa;
    font-size: 1.25rem;
    margin: 0 1rem;
    position: relative;
    padding-bottom: 0.8125rem;
    cursor: pointer;
}

.login .active {
    color: #000000;
}

.login .active:after {
    content: '';
    width: 100%;
    height: 0.25rem;
    background: rgba(0, 0, 0, 1);
    border-radius: 0.125rem;
    position: absolute;
    left: 0;
    bottom: 0;
}
.el-form-item {
}

.layui-form-item {
    margin: 3.75rem auto !important;
    width: 30.875rem;
    height: 5rem;
    border-radius: 40px;
}
.login .el-input--medium .el-input__inner {
    height: 5rem;
    line-height: 5rem;
    border-radius: 40px;
    overflow: hidden;
}

.login .reg-wrap .layui-form-items {
    margin: 2.5rem 0 !important;
}

.reg-wrap .login-auto {
    // margin: 0 9.375rem 0 9.0625rem !important;
}

.layui-form-item .login-list {
    width: 100%;
    height: 5rem;
    line-height: 5rem;
    background: #f8f8f8;
    border-radius: 2.5rem;
    margin: 0 auto;
    float: inherit;
}

.layui-form-item .login-list .layui-input {
    width: 100%;
    height: 100%;
    padding: 0 2.5rem;
    font-size: 1.375rem;
    font-family: SourceHanSansCN-Regular, SourceHanSansCN;
    font-weight: 400;
    color: #cbcbcb;
    background: none;
    border: none;
}

.login-list .hzm {
    width: 19.6875rem !important;
    float: left;
}

.login .hqyzm {
    font-size: 1.375rem;
    color: #3893f3;
    float: left;
    position: absolute;
    top: 7px;
    right: 20px;
    background: transparent;
    border: 0;
    cursor: pointer;
}

.login-list .hqyzm::after {
    content: '';
    width: 0.125rem;
    height: 3.5rem;
    background: rgba(234, 234, 234, 1);
    position: absolute;
    top: 50%;
    margin-top: -1.75rem;
    left: 0;
}

.login-auto {
    margin: 0 auto !important;
}

.login-auto .layui-input-inline input[type='checkbox'] {
    width: 0.9375rem;
    height: 0.9375rem;
    background: #f4f4f4;
    margin-right: 0.625rem;
    font-size: 1.125rem;
    color: #333333;
}

.layui-input-inline {
    float: left;
}
.wjmm {
    font-size: 14px;
    color: #333333;
    display: inline-block;
    cursor: pointer;
}

.login-btn {
    display: block;
    width: 30.875rem;
    height: 5rem;
    background: #3e95f0;
    border-radius: 2.5rem;
    margin: 30px auto 0 auto;
    font-size: 1.625rem;
    font-family: SourceHanSansCN-Medium, SourceHanSansCN;
    font-weight: 500;
    color: #ffffff;
    border: none;
}

.login-auto .qtfs {
}

.login-qtfs {
    margin: 3.625rem 9.375rem 0 7.75rem !important;
}

.login-qtfs .qtfs {
    width: 200px !important;
    line-height: 20px;
    display: inline-block;
}
.login-auto .qtfs > span {
    line-height: 20px;
    display: inline-block;
}
.login-qtfs .qtfs a {
    margin-left: 1.5625rem;
    margin-top: 3px;
    display: inline-block;
    position: relative;
    top: 3px;
}

.login-qtfs .qtfs a img {
    height: 1.625rem;
}

.zhdl {
    text-align: center;
}

.reg-wrap {
    /* height: 62.4375rem; */
    /* margin-top: 2.5rem; */
    padding-bottom: 1.875rem;
}

input::-webkit-input-placeholder {
    /*Webkit browsers*/
    color: #cbcbcb;
}

input:-moz-placeholder {
    /*Mozilla Firefox 4 to 8*/
    color: #cbcbcb;
}

input::moz-placeholder {
    /*Mozilla Firefox 19+*/
    color: #cbcbcb;
}

input:-ms-input-placeholder {
    /*Internet Explorer 10+*/
    color: #cbcbcb;
}
.dlfs-wrap {
    display: none;
}
.inputStyle input {
    background: #f8f8f8 !important;
}
.verification {
    position: relative;
}
#qrcode {
    width: 180px;
    height: 180px;
    margin: 45px auto 0px;
    display: block;
}
#qrcode img {
    width: 100%;
    height: 100%;
}
</style>
